package com.wjh.www.entity.dto;

import com.fasterxml.jackson.annotation.JsonInclude;

import static com.wjh.www.entity.dto.ResultConstant.*;


/**
 * 公共返回信息
 *
 * @author wjh
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResult<T> {
    /**
     * 状态码
     */
    private int code;
    /**
     * 附加信息
     */
    private String message;
    /**
     * 操作是否成功
     */
    private boolean isSuccess;
    /**
     * 传给前端的内容
     */
    private T data;
    /**
     * 默认传输json
     */
    public TypeEnum typeEnumEnum = TypeEnum.JSON;

    public CommonResult() {
    }

    public CommonResult(int code, String message, boolean isSuccess) {
        this.code = code;
        this.message = message;
        this.isSuccess = isSuccess;
    }

    public CommonResult(int code, String message, boolean isSuccess, T data) {
        this.code = code;
        this.message = message;
        this.isSuccess = isSuccess;
        this.data = data;
    }

    public CommonResult(int code, String message, boolean isSuccess, T data, TypeEnum typeEnumEnum) {
        this.code = code;
        this.message = message;
        this.isSuccess = isSuccess;
        this.data = data;
        this.typeEnumEnum = typeEnumEnum;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <P> CommonResult<P> operateSuccess() {
        return new CommonResult<P>(SUCCESS_CODE, OPERATE_SUCCESS_MESSAGE, true, null);
    }

    public static <P> CommonResult<P> operateSuccess(P p) {
        return new CommonResult<P>(SUCCESS_CODE, OPERATE_SUCCESS_MESSAGE, true, p);
    }

    public static <P> CommonResult<P> operateFail(P p) {
        return new CommonResult<P>(FAIL_CODE, OPERATE_FAIL_MESSAGE, false, p);
    }

    public static <P> CommonResult<P> operateFailWithMessage(String message) {
        return new CommonResult<P>(FAIL_CODE, message, false);
    }

    public static <P> CommonResult<P> operateNoPower(P p) {
        return new CommonResult<P>(FORBIDDEN_CODE, OPERATE_NO_POWER, false, p);
    }

    public static <P> CommonResult<P> operateSuccessWithFile(P p) {
        return new CommonResult<P>(SUCCESS_CODE, OPERATE_SUCCESS_MESSAGE, true, p, TypeEnum.FILE);
    }

    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", isSuccess=" + isSuccess +
                ", data=" + data +
                ", typeEnumEnum=" + typeEnumEnum +
                '}';
    }
}
